home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 11 / develop 11 code / Graphical Truffles / Blasto code / Blasto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-16  |  3.6 KB  |  136 lines  |  [TEXT/MPS ]

  1. /*
  2.     Blasto
  3.     A small program that blasts an 8 bit color icon to the screen.
  4.     3-6-92         By Brigham Stevens
  5.                 Apple Developer Technical Support
  6.     3-13-92    - BRS - Uh Oh, it's Friday the 13th
  7.                 Cleaned up & commented code here and there.
  8.     4-13-92 - BRS - changed icon to be self-erasing
  9.                 Made code somewhat better by changing names and stuff.
  10.                 Added comments
  11.     4-24-92 - BRS - Added ifdefs around timing code, changed names again.
  12.     11-11-92 - BRS - made a THINK C project file, and made this code work with 
  13.                     THINK C.  Also changed the date in the comment above.
  14. */
  15.  
  16. #ifndef THINK_C
  17. #include <OSUtils.h>
  18. #include <Windows.h>
  19. #include <QDOffscreen.h>
  20. #include <Retrace.h>
  21. #include <Memory.h>
  22. #include <Resources.h>
  23. #include <Events.h>
  24. #include <Menus.h>
  25. #endif
  26.  
  27. #include "DirectScreen.h"
  28.  
  29. void InitToolBox(short numMoreMasters);
  30.  
  31. /* Amount to move icons these need to be around 1 or 2    */
  32. /*    or our icon will not be self erasing */
  33. #define ROW_OFF    1
  34. #define COL_OFF 1
  35.  
  36. #define MAX_SECONDS 100
  37.  
  38. #ifndef THINK_C
  39. void ShowResult(long frameCount);
  40. long AverageTable(long *tab, short count);
  41. #endif
  42.  
  43. void main()
  44. {
  45.     WindowPtr        directWindow;
  46.     GrafPort        killMenuBar;
  47.     Rect            screenRect;
  48.     GDHandle        mainScreen;
  49.     PixMapHandle    mainscreenPixMap;
  50.     Handle            colorIconHandle;
  51.     Point            plotLocation;
  52.     short            vDelta = ROW_OFF;
  53.     short            hDelta = COL_OFF;
  54.     long            frameCount;
  55.     long            *ticks = (long*)0x16A;
  56.     long            beforeTicks;    
  57.     long            tickTable[MAX_SECONDS];
  58.     short            tickIndex = 0;
  59.  
  60.     /* Standard Witch Chant enclosed... */
  61.     InitToolBox(4);        /* 4 calls to MoreMasters */
  62.  
  63.     /* get a handle to the color icon we are drawing */
  64.     colorIconHandle = GetResource('icl8',128);
  65.     if(!colorIconHandle) {
  66.         SysBeep(5);
  67.         DebugStr("\pBuild problems: 'icl8' not loaded.");
  68.         return;
  69.     }
  70.  
  71.     /* cover up the part of the screen we are writing on */
  72.     /* with a window, so other apps will not mess with our */
  73.     /* animation via update events in the background */
  74.     screenRect = qd.screenBits.bounds;
  75.     directWindow = NewWindow(nil,&screenRect,nil,true,plainDBox,nil,false,0L);
  76.  
  77.     /* This covers up the menu bar */
  78.     /* and fills the screen with white */
  79.     OpenPort((GrafPtr)&killMenuBar);
  80.     EraseRect(&killMenuBar.portRect);
  81.     
  82.     /* get the main screen's Pix map */
  83.     /* Which this program expects to be 8 bits deep */
  84.     mainScreen = GetMainDevice();
  85.     mainscreenPixMap = (**mainScreen).gdPMap;
  86.             
  87.     /* Set up a safe rectangle for bouncing off the screen */
  88.     screenRect.top = 4;
  89.     screenRect.left = 4;
  90.     screenRect.bottom -= 32;
  91.     screenRect.right -= 32;
  92.  
  93.  
  94.     /* loop until the mouse is clicked */
  95.     /* drawing and moving the color icon */
  96.  
  97.     
  98.     for(tickIndex = 0; tickIndex < MAX_SECONDS; tickIndex++) {
  99.         /* This is where to start drawing the color icon*/
  100.         plotLocation.h = 100;
  101.         plotLocation.v = 100;
  102.         frameCount = 0;
  103.         beforeTicks = *ticks;
  104.         while((*ticks - beforeTicks) < 60) {
  105.     
  106.             /* update the location of the icon */
  107.             plotLocation.v += vDelta;
  108.             plotLocation.h += hDelta;
  109.             /* This forces us to stay on the screen and prevent bus errors */
  110.             if(!PtInRect(plotLocation,&screenRect)) {
  111.                 vDelta = -vDelta;    /* swap the deltas to bounce of corner */
  112.                 hDelta = -hDelta;
  113.             }
  114.     
  115.             /* draw the icon */
  116.             
  117.             DirectPlotColorIcon((long *)*colorIconHandle, 
  118.                         mainscreenPixMap, plotLocation.v, plotLocation.h);
  119.             frameCount++;
  120.             /* this was not part of the timing code, but */
  121.             /* I want to be able to bail out early */
  122.             if(Button()) ExitToShell();
  123.         }
  124.         tickTable[tickIndex] = frameCount;
  125.     }
  126.     
  127.     /* show the average number of frames per second for MAX_SECONDS seconds */
  128. #ifndef THINK_C    
  129.     ShowResult( AverageTable(tickTable,MAX_SECONDS) );
  130. #endif
  131.         
  132.     CloseWindow(directWindow);
  133.     ClosePort((GrafPtr)&killMenuBar);
  134.     DrawMenuBar();
  135.     
  136. }